let app = getApp()
const db = wx.cloud.database({
  env: app.globalData.CLOUD_ENV
})
const DBClassify = db.collection('db_classifys')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    ctype: {
      type: String,
      value: ''
    }
  },
  // 监听器
  observers: {
    ctype: function (value) {
      if (value) {
        // console.log("value:",value)
        // console.log('x:',value)
        // let key = "_"+value;
        // let _start = "_"+value+"_time_";
        // this.setData({key, _start})
        // let str = wx.getStorageSync(key)
        // let _start_time = wx.getStorageSync(_start)
        // if(str){
        //   if(_start_time){
        //     let _end_time = new Date().getTime();
        //     if((_end_time - _start_time) > 4 * 60 * 60 * 1000){
        //       this.listClassifyFn2(value);
        //     }
        //   }
        //   let classifys = JSON.parse(str)
        //   this.setData({classifys})
        // }
        // else {
        //   this.listClassifyFn2(value);
        // }
        this.listClassifyFn2(value);
      }
    }
  },

  lifetimes: {
    attached() {
      //  初始化
      // this.listClassifyFn();
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    classifys: [],
    num: 0,
    direction: 'x', //x
    key: '',
    start_time: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showListFn() {
      let direction = this.data.direction;
      if (direction == 'y') {
        direction = 'x';
      } else {
        direction = 'y';
      }
      this.setData({
        direction
      })
    },

    async listClassifyFn2(type) {
      // console.log("type:",type)
      let res = await DBClassify.where({
        type
      }).count()
      let count = res.total;
      // console.log({count})
      let all = [];
      for (let i = 0; i < count; i += 20) {
        await DBClassify.where({type}).skip(i).get()
          .then(
            content => {
              let {
                data
              } = content;
              if (data.length != 0) {
                all = all.concat(data)
              }
            }
          )
      }
      this.setData({
        classifys: all 
      })
      // console.log(all);

      // let that = this;
      // await DBClassify.where({type}).get()
      //   .then(
      //     content => {
      //       // console.log('3.0', content)
      //       // let key = that.data.key;
      //       // let start_time = that.data.start_time;

      //       if (content.data) {
      //         that.setData({
      //           classifys: content.data
      //         })
      //         // wx.setStorageSync(key, JSON.stringify(content.data))
      //         // wx.setStorageSync(start_time, new Date().getTime())

      //       }
      //     }
      //   )
    },
  }
})